﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iPal.analysis;
using iPal.lexer;
using iPal.node;
using iPal.parser;

namespace Sablecc
{
    public class SemanticException : ApplicationException
    {
        public SemanticException(String message)
            : base(message)
        {
        }
    }

    class SemanticAnalyser : DepthFirstAdapter
    {
        private Hashtable _symbolTable = new Hashtable();

        public Hashtable SymbolTable { get { return _symbolTable; } }

        public override void OutAExprAstVarAssign(AExprAstVarAssign node)
        {

        }

        public override void OutAIdentifierAstExpr(AIdentifierAstExpr node)
        {
            if (!_symbolTable.ContainsKey(node.GetId().Text))
            {
                throw new SemanticException("Id doesn't exsist");
            }
        }

        public override void OutAInitAstVarAssign(AInitAstVarAssign node)
        {
            if (!_symbolTable.ContainsKey(node.GetId().Text))
            {
                _symbolTable.Add(node.GetId().Text, node.GetAstType());
            }
            else throw new SemanticException("Id already exsists");

        }

        public override void OutADeclAstVarAssign(ADeclAstVarAssign node)
        {
            if (!_symbolTable.ContainsKey(node.GetId().Text))
            {
                _symbolTable.Add(node.GetId().Text, node.GetAstType());
            }
            else throw new SemanticException("Id already exsists");
        }


        #region helpFuncs
        #endregion

    }
}
